package com.tingyukeji.fastpay.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;

/**
 *
 */

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
    protected List<T> mlist;
    protected Context context;
    private OnItemClickListener mClickListener;
    protected OnViewClickListener viewClickListener;
    protected View mHeaderView;
    protected final int TYPE_HEADER = -100;
    protected final int TYPE_NORMAL = -101;

    public void setMlist(List<T> mlist) {
        this.mlist = mlist;
//        notifyItemRangeChanged(0,mlist.size());
        notifyDataSetChanged();
    }

    public List<T> getMlist() {
        return mlist;
    }

    public BaseRecyclerAdapter(List<T> mlist, Context context) {
        this.mlist = mlist;
        this.context = context;
    }

    public BaseRecyclerAdapter(List<T> mlist, Context context, boolean enable) {
        this.mlist = mlist;
        this.context = context;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) return new BaseViewHolder(mHeaderView);
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        final BaseViewHolder holder = new BaseViewHolder(inflater.inflate(getItemLayoutId(viewType), parent, false));
        if (null != mClickListener && null != holder) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mClickListener.onItemClick(holder.itemView, getRealPosition(holder));
                }
            });
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) return;
        bindData(holder, getRealPosition(holder), mlist.get(getRealPosition(holder)));
    }

    @Override
    public int getItemCount() {
        return mHeaderView == null ? mlist.size() : mlist.size() + 1;
    }

    public void add(int pos, T item) {
        mlist.add(pos, item);
        notifyItemInserted(pos);
    }

    public void delete(int pos) {
        mlist.remove(pos);
//        notifyItemRemoved(pos);
        notifyDataSetChanged();
    }

    abstract protected int getItemLayoutId(int viewType);

    abstract protected void bindData(BaseViewHolder holder, int position, T item);

    public interface OnItemClickListener {
        void onItemClick(View itemView, int pos);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mClickListener = listener;
    }

    public interface OnViewClickListener {
        void onViewClick(View itemView, int pos);
    }

    public void setOnViewClickListener(OnViewClickListener listener) {
        this.viewClickListener = listener;
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null) return TYPE_NORMAL;
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }
}
